package com.cskaoyan.service;

import com.cskaoyan.bean.MarketStorage;
import com.cskaoyan.bean.MarketStorageExample;
import com.cskaoyan.bean.common.BasePageInfo;
import com.cskaoyan.bean.common.CommonData;
import com.cskaoyan.mapper.MarketStorageMapper;
import com.cskaoyan.utils.StringUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Fyy
 * @since 2022/12/28 22:42
 */

@Service
public class StorageServiceImpl implements StorageService {

    @Autowired
    MarketStorageMapper storageMapper;

    @Override
    public void create(MarketStorage marketStorage) {
        storageMapper.insertSelective(marketStorage);
    }

    /**
     * 查找对象信息
     *
     * @param info
     * @param key
     * @param name
     * @return
     */
    @Override
    public CommonData<MarketStorage> getList(BasePageInfo info, String key, String name) {
        //开启分页
        PageHelper.startPage(info.getPage(), info.getLimit());
        //实例化对象
        MarketStorageExample example = new MarketStorageExample();
        //先排序
        example.setOrderByClause(info.getSort() + " " + info.getOrder());
        //增加条件查询
        MarketStorageExample.Criteria criteria = example.createCriteria();
        // 查询逻辑存在,表中deleted字段值为1是删除状态
        criteria.andDeletedEqualTo(false);
        //对数据进行判空，如果数据是null则此sql语句不执行
        if (!StringUtil.isEmpty(key)) {
            criteria.andKeyLike("%" + key + "%");
        }
        if (!StringUtil.isEmpty(name)) {
            criteria.andNameLike("%" + name + "%");
        }
        //获取对象表
        List<MarketStorage> list = storageMapper.selectByExample(example);
        //封装
        PageInfo<MarketStorage> pageInfo = new PageInfo<>(list);


        return CommonData.data(pageInfo);
    }


    /**
     * 删除对象
     *
     * @param marketStorage
     */
    @Override
    public void deleteMarketStorage(MarketStorage marketStorage) {
        //获得要删除的对象id
        Integer id = marketStorage.getId();
        // 获得 要删除的对象的逻辑删除标志
        Boolean deleted = marketStorage.getDeleted();
        // 通过id 更新 删除标记
        MarketStorage storageDelete = new MarketStorage();
        storageDelete.setId(id);
        // 删除标记取反
        storageDelete.setDeleted(!deleted);
        //删除
        storageMapper.updateByPrimaryKeySelective(storageDelete);
    }

    /**
     * 修改对象，仅修改对象的名字
     */
    @Override
    public void updateMarketStorage(MarketStorage mrketStorage) {
        mrketStorage.setKey(null);
        storageMapper.updateByPrimaryKeySelective(mrketStorage);
    }
}
